<?php

namespace App\Admin\Controllers;

use App\Member;
use App\Withdraw;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class WithdrawController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        \Auth::loginUsingId(1);
        return Admin::content(function (Content $content) {

            $content->header('提现申请');
            $content->description('提现申请');

            $content->body($this->grid());
        });
    }
    public function userWithdraw($member_id){
        return Admin::content(function (Content $content)use($member_id) {
            $member=Member::find($member_id);
            $content->header($member->nickname.'提现详情');
            $content->description($member->nickname.'提现详情');

            $content->body(Admin::grid(Withdraw::class, function (Grid $grid) use($member_id)           {
                $grid->model()->where('member_id','=',$member_id)->where('status','=',Withdraw::SURED);
                $grid->username('提现人');
                $grid->bank_id('银行卡号');
                $grid->mobile('手机号');
                $grid->price('提现金额');
                $grid->apply_time('提现时间')->sortable();
                $grid->pay_time('汇款时间')->sortable();
                $grid->disableCreateButton();
                $grid->tools(function($tools){
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                    });
                });
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->disableFilter();
                $grid->disableExport();
            }));
        });
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Withdraw::class, function (Grid $grid) {
            $grid->model()
                ->leftJoin('members','members.id','=','withdraw.member_id')
                ->select('withdraw.*','members.nickname','members.mobile')
                ->orderBy('apply_time','desc');
            $grid->username('提现人');
            $grid->bank_id('银行卡号');
            $grid->nickname('用户昵称');
            $grid->mobile('手机号');
            $grid->price('提现金额')->display(function($value){
                return $this->poundage+$value;
            });
            $grid->poundage('手续费');
            $grid->status('状态')->display(function($value){
                return Withdraw::$statusMap[$value];
            });
            $grid->apply_time('提现时间');
            $grid->approve_time('审批时间');
            $grid->pay_time('汇款时间');
            $grid->refuse_time('拒绝时间');
            $grid->filter(function ($filter){
                $filter->disableIdFilter();
                $filter->like('username','提现人');
                $filter->equal('bank_id','银行号');
                $filter->like('nickname','用户昵称');
                $filter->equal('mobile','手机号');
                $filter->where(function($query){
                    $query->where('withdraw.status', '=', $this->input);
                },'状态')->select(Withdraw::$statusMap);
                $filter->between('apply_time','提现时间')->datetime();
                $filter->between('approve_time','审批时间')->datetime();
                $filter->between('pay_time','汇款时间')->datetime();
            });
            $grid->disableCreateButton();
            $grid->tools(function($tools){
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->actions(function($actions){
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append("<a class='btn btn-xs btn-success' href='/admin/finance/withdraw/read/{$actions->getKey()}'  target='_blank'>查看</a>");
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Withdraw::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
